<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    private $_id;

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        $api = new Api('account');
        $params = array(
            'UserLogonReq' => array(
                'userAccount' => $this->username,
                'password' => $this->password
            )
        );
        
        $row = $api->apiclient('/LdAccountServer/userAccount/userLogon', $params);
        if ($row == 0) {
            throw new CHttpException(404, '网络异常,请稍后再试21212');
        }

        if ($row['code'] == 0) {
            $this->_id = $row['UserLogonRsp']['userId'];
            $params_id = array(
                'GetUserInfoReq' => array(
                    'userId' => $this->_id
                )
            );

            $row_id = $api->apiclient('/LdAccountServer/userAccount/getUserInfo', $params_id);
            if ($row_id == 0) {
                throw new CHttpException(404, '网络异常,请稍后再试');
            }
            if ($row_id['code'] == 0) {
                $this->setInfo($row_id['GetUserInfoRsp']);
                $this->username = $row_id['GetUserInfoRsp']['userAccount'];
            }
            $this->errorCode = self::ERROR_NONE;
        } else {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            $this->errorMessage = $row['ExceptionRsp']['msg'];
        }

        return $this->errorCode == self::ERROR_NONE;
    }

    public function register() {
        $api = new Api('account');
        $params = array(
            'RegUserReq' => array(
                'userAccount' => $this->username,
                'email' => $_POST['LoginForm']['email'],
                'password' => $this->password,
                'phone' => $_POST['LoginForm']['mobile']
            )
        );
        if ($this->password == $_POST['LoginForm']['password_repeat']) {
            $row = $api->apiclient('/LdAccountServer/userAccount/regUser', $params);
            if ($row == 0) {
                throw new CHttpException(404, '网络异常,请稍后再试');
            }
//            print_r($row);
            if ($row['code'] == 0) {
                $this->authenticate();
            } else {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
                $this->errorMessage = $row['ExceptionRsp']['msg'];
                return $this->errorCode == self::ERROR_NONE;
            }
        } else {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            $this->errorMessage = '两次密码不一致';
            return $this->errorCode == self::ERROR_NONE;
        }
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId() {
        return $this->_id;
    }

    public function setInfo($info) {
        $this->setState('info', $info);
    }

}
